<!-- 后台编辑应用页面 -->
<link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
<link rel="stylesheet" href="/js/lay-module/formSelects/formSelects.css" media="all">
<link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<link rel="stylesheet" href="/css/public.css" media="all">
<style>
    #upload{
        height: 120px;
        width: 120px;
    }
    #upload img{
        position: absolute;
        width: 120px;
        height: 120px;
    }
    .layui-upload-list{
        margin: 0 !important
    }
    .layui-upload-drag{
        position: absolute;
        padding: 31px;
        border: none;
    }
</style>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="uk-layui-form">
            <form class="layui-form uk-form" action="#">
                <#if data??><input type="hidden" name="id" value="${data.id}"></#if>
                <div class="layui-form-item">
                    <label class="layui-form-label">学号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="uid" class="layui-input" lay-verify="required"
                            <#if data??>value="${data.sid!''}"</#if> placeholder="请输入学号" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名：</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" class="layui-input" lay-verify="required"
                            <#if data??>value="${data.name!''}"</#if> placeholder="请输入姓名" autocomplete="off">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱：</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" class="layui-input" lay-verify="required"
                            <#if data??>value="${data.sid!''}"</#if> placeholder="请输入一个您可供持久联系的电子邮箱" autocomplete="off">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">联系方式：</label>
                    <div class="layui-input-block">
                        <input type="text" name="teleNumber" class="layui-input" lay-verify="required"
                            <#if data??>value="${data.sid!''}"</#if> placeholder="请输入一个您的联系方式" autocomplete="off">
                    </div>
                </div>

                
                <div class="layui-form-item">
                    <label class="layui-form-label">照片上传：</label>
                    <div class="layui-input-block">
                        <div id="upload">
                            <div class="layui-upload-list">
                                <button type="button" class="layui-upload-drag">
                                    <i class="layui-icon">&#xe67c;</i>
                                </button>
                                <img id="avatar">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <input type="hidden" id="gender" name="gender" value="true"/>
                    <label class="layui-form-label">性别：</label>
                    <div class="layui-input-block">
                        <input type="checkbox" id="checkbox" lay-skin="switch" lay-filter="change" lay-text="男|女"
                            <#if data??><#if data.gender==true>checked</#if></#if> >
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">入学时间：</label>
                    <div class="layui-input-block">
                        <input type="text" id="enrollDate" name="enrollDate"
                        <#if enrollDate??>value="${enrollDate?string("yyyy-MM-dd")}"</#if>
                            placeholder="请选择入学时间" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">生  日：</label>
                    <div class="layui-input-block">
                        <input type="text" id="birthDate" name="birthDate"
                        <#if birthDate??>value="${birthDate?string("yyyy-MM-dd")}"</#if>
                            placeholder="请选择出生日期" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">班级：</label>
                    <div class="layui-input-block">
                        <select name="classId" id="classId" lay-verify="required">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>

                <!-- 按钮 -->
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" lay-submit lay-filter="release">更新</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery', 'laydate', 'httpClient','miniTab', 'notice', "upload"], function(){
        var form = layui.form,
            laydate = layui.laydate,
            httpClient = layui.httpClient,
            miniTab = layui.miniTab,
            upload = layui.upload,
            notice = layui.notice,
            $ = layui.jquery;

        // 日期
        laydate.render({
            elem: '#enrollDate',
            type:'date',
            format: 'yyyy-MM-dd',
            trigger: 'click'
        });

        laydate.render({
            elem: '#birthDate',
            type:'date',
            format: 'yyyy-MM-dd',
            trigger: 'click'
        });

        form.on('switch(change)', function(data){
            $('#gender').val(data.elem.checked?'1':'0');
        });

        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,url: '/upload/' //上传接口
            ,done: function(res){
                //上传完毕回调
                if (res.code==200) {
                    notice.success(res.msg);
                }

                if (res.code==500) {
                    notice.error(res.msg);
                }

                $('#avatar').attr('src', res.data);
            }
            ,error: function(){
                //请求异常回调
            }
        });
        
        // 动态渲染select
        httpClient.getWithoutNotice('/class/listAll',{},(response)=>{
            let list = response.data;
            let select = document.getElementById("classId");
            if (list!=null || list.size()>0) {
                list.forEach(major => {
                    let option = document.createElement("option");
                    option.setAttribute("value", major.id);
                    option.innerText = major.className;
                    select.appendChild(option);
                });
            }
            form.render('select');
        });

        // 更新
        form.on('submit(release)', function(form){
            let data = form.field;
            data.avatar = $('#avatar')[0].src;

            httpClient.put('/user/update', JSON.stringify(data), (response)=>{
                layer.alert(response.msg, { icon: 1, closeBtn: 0 }, function(index) {
                    //关闭弹窗
                    layer.close(index);
                    miniTab.deleteCurrentByIframe();
                });
        });
            return false;
        });
    });
</script>